import Vue from 'vue'
import Vuex from 'vuex'
import config from '@/js/util/config.js';
import situation from './situation.js';

Vue.use(Vuex);
export default new Vuex.Store({
    state: {
        clickInfo: {},
        mapActive: 1,
        callbackIndex: 0,
        __version: config.data.version,
        toolbarId: '',
        viewportSize: {
            width: 0,
            height: 0
        }
    },
    getters: {
        mapActive: state => state.mapActive,
        toolbarId: state => state.toolbarId
    },
    mutations: {
        setToolbarId(state, id) {
            state.toolbarId = id;
        },
        // 鼠标左键点击时触发此事件 获取信息
        setClickInfo(state, object) {
            state.clickInfo = object;
        },
        // 设置主界面UI元素的可见性
        async setMainUIVisibility(state, visible) {
            await window.__g.settings.setMainUIVisibility(visible);
        },
        // 设置交互开关，目前支持启用和禁用鼠标交互，禁用后可以通过API设置交互
        async setEnableInteract(state, flag) {
           await window.__g.settings.setEnableInteract(flag);
        },
        setMapActive(state, index) {
            state.mapActive = index;
        },
        // 重置
        async setReset(state, type) {
            await window.__g.reset(1);
            await window.__g.reset(2);
        },
        // 获取三维窗口
        setViewportSize(state, array) {
            state.viewportSize.width = array[0] || 0;
            state.viewportSize.height = array[1] || 0;
        },
        //最小化窗口
        setViewWebUIMinSize(state, obj) {
            const param = {
                command: 513,
                timestamp: new Date().getTime(),
                callbackIndex: state.callbackIndex,
                __command: 'WebUI_Minimize',
                __version: state.__version
            }
            window.__g.call(param);
        },
        //最大化窗口
        setViewWebUIMaxSize(state, obj) {
            const param = {
                command: 514,
                timestamp: new Date().getTime(),
                callbackIndex: state.callbackIndex,
                __command: 'WebUI_Maximize',
                __version: state.__version
            }
            window.__g.call(param);
        },
        //恢复窗口
        setViewWebUIRestoreSize(state, obj) {
            const param = {
                command: 515,
                timestamp: new Date().getTime(),
                callbackIndex: state.callbackIndex,
                __command: 'WebUI_Restore',
                __version: state.__version
            }
            window.__g.call(param);
        },
        //进入全屏
        setViewWebUIEnterFullScreen(state, obj) {
            const param = {
                command: 530,
                timestamp: new Date().getTime(),
                callbackIndex: state.callbackIndex,
                __command: 'WebUI_EnterFullScreen',
                __version: state.__version
            }
            window.__g.call(param);
        },
        //退出全屏
        setViewWebUIExitFullScreen(state, obj) {
            const param = {
                command: 531,
                timestamp: new Date().getTime(),
                callbackIndex: state.callbackIndex,
                __command: 'WebUI_ExitFullScreen',
                __version: state.__version
            }
            window.__g.call(param);
        }
    },
    actions: {
        // 获取导览列表
        getAnimationList({ commit }) {
            return new Promise(async(resolve, reject) => {
                let data = await window.__g.camera.getAnimationList();
                resolve(data);
            })
        },
        // 获取相机位置
        getCameraLocation() {
            return new Promise(async(resolve, reject) => {
                let data = await window.__g.camera.get();
                resolve(data);
            })
        },
        // 跟acp工程绑定 set内容
        setChartsParams({ commit, state }, obj) {
            return new Promise(async(resolve, reject) => {
                if (window.__g && window.__g.call) {
                    state.callbackIndex++;
                    const param = {
                        command: 493,
                        saveJsonString: JSON.stringify(obj).replace(/"/g,"~!@~!@~!@"),
                        timestamp: new Date().getTime(),
                        callbackIndex: state.callbackIndex,
                        __command: 'WebUIJson_Set',
                        __version: state.__version
                    }
                    let data = await window.__g.call(param);
                    if (data.result == '0') {
                        resolve();
                    } else {
                        reject('接口失败');
                    }
                } else {
                    reject('接口不存在');
                }
            })
        },
        // 存放在文件夹下面 ，一个电脑一个
        setChartsItem({ commit, state }, obj) {
            return new Promise(async(resolve, reject) => {
                if (window.ue) {
                    window.ue.internal.execute('setItem', 'charts_params', JSON.stringify(obj));
                    resolve();
                }
            });
        },
        // 跟acp工程绑定 get内容
        getChartsParams({ commit, state }) {
            return new Promise(async(resolve, reject) => {
                const param = {
                    command: 492,
                    timestamp: new Date().getTime(),
                    callbackIndex: state.callbackIndex,
                    __command: 'WebUIJson_Get',
                    __version: state.__version
                }
                if (window.__g && window.__g.call) {
                    let data = await window.__g.call(param);
                    if (data.result == '0' && data.data[0]) {
                        if (data.data[0].hasOwnProperty("saveJsonString")) {
                            if (typeof data.data[0].saveJsonString == 'string') {
                                try {
                                    resolve(JSON.parse(data.data[0].saveJsonString))
                                } catch (error) {
                                    resolve('{}');
                                }
                            }
                        } else {
                            resolve('{}');
                        }
                    } else {
                        resolve('{}');
                    }
                } else {
                    resolve('{}');
                }
            })
        },
        // 存放在文件夹下面 ，一个电脑一个
        getChartsItem({ commit, state }) {
            return new Promise(async(resolve, reject) => {
                let val = await window.ue.internal.execute('setItem', 'charts_params');
                if (JSON.parse(val)) {
                    resolve(JSON.parse(val))
                } else {
                    resolve('{}');
                }
            })
        }
    },
    modules: {
        situation: situation
    }
})
